/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.util {
	import com.googlecode.milib.vo.RGB;

	import flash.geom.Point;

	/**
	 * @author Marek Brun
	 */
	public class MLMathUtil {

		public static var toRadians:Number=Math.PI/180;
		public static var toAngles:Number=180/Math.PI;
		
		public static function getNextPrev(min:Number, max:Number, currentNumber:Number, isNext:Boolean):Number {
			if(isNext){
				currentNumber++;
				if(currentNumber>max){ return min; }
				return currentNumber;
			}else{
				currentNumber--;
				if(currentNumber<min){ return max; }
				return currentNumber;
			}
		}
		
		public static function dist(x1:Number, y1:Number, x2:Number, y2:Number):Number{
			var oxp:Number=x2-x1;
			var oyp:Number=y2-y1;
			return Math.sqrt(oxp*oxp+oyp*oyp);
		}
		
		public static function pointsDist(point0:Point, point1:Point):Number{
			return dist(point0.x, point0.y, point1.y, point1.y);
		}
		
		public static function getHEXByRGB(r:Number, g:Number, b:Number):Number {
			return r<<16 | g<<8 | b;
		}
		
		public static function getRGBByHEX(hex:Number):RGB {
	  		return new RGB(hex>>16, Math.floor(hex/256)%256, hex%256);
		}
		
		public static function getRescaleToRect(toRectWidth:Number, toRectHeight:Number, width:Number, height:Number, isInside:Boolean=true):Number {
			if(isInside){
				if(toRectWidth/toRectHeight>width/height) {
					return toRectHeight/height;
				}else{
					return toRectWidth/width;
				}
			}else{
				if(width/height>toRectWidth/toRectHeight) {
					return toRectHeight/height;
				}else{
					return toRectWidth/width;
				}
			}
		}
		
		public static function getXYAlignMidAfterRescaleToRect(toRectWidth:Number, toRectHeight:Number, width:Number, height:Number):Point {
			if(toRectWidth/toRectHeight>width/height){
				return new Point(toRectWidth/2-(width*(toRectHeight/height))/2, 0);
			}else{
				return new Point(0, toRectHeight/2-(height*(toRectWidth/width))/2);
			}
		}
		
		public static function getAngleByPoints(x0:Number, x1:Number, y0:Number, y1:Number):Number {
			return Math.atan2(y1-y0,x1-x0)*(180/Math.PI);
		}
		
		public static function scopeRoll(ini:Number, end:Number, n:Number):Number {
			var dist:Number=end-ini;
			if(n<0){
				n+=dist*(int((-n)/dist)+1);
			}
			
			return ini+dist*((n%dist)/dist);
		}
		
		static public function getMirrorSin(num:Number):Number {
			if(num>0){
				return 1-(Math.sin((1-num)*Math.PI/2));
			}else{
				return -(1-(Math.sin((1-num)*Math.PI/2)));
			}
			
		}
		
		static public function isLinesCross(x11:Number, y11:Number, x12:Number, y12:Number, x21:Number, y21:Number, x22:Number, y22:Number):Boolean {
			var cross:Point=getLinesCrossPoint(x11, y11, x12, y12, x21, y21, x22, y22);
//			if(cross.x>x11 && cross.x<x12 && cross.y>y11 && cross.y<y12){
//				return true;
//			}
			if((cross.x>x11 && cross.x<x12) || (cross.x<x11 && cross.x>x12)){
				if((cross.x>x21 && cross.x<x22) || (cross.x<x21 && cross.x>x22)){
					return true;
			}}
			return false;
		}
		
		static public function getLinesCrossPoint(x11:Number, y11:Number, x12:Number, y12:Number, x21:Number, y21:Number, x22:Number, y22:Number):Point {
			var va0:Number = x12*y11-x11*y12;
			var a1:Number = (y11-y12)/va0;
			var b1:Number = (x12-x11)/va0;
			var va:Number = x22*y21-x21*y22;
			var a2:Number = (y21-y22)/va;
			var b2:Number = (x22-x21)/va;
			return new Point((b1-b2)/(a2*b1-a1*b2), (a1-a2)/(b2*a1-b1*a2));
		}
		
	}
}
/*
mt.isLinesCross=function(ln11x, ln11y, ln12x,ln12y, ln21x,ln21y, ln22x,ln22y, wnCross){
	if(!wnCross){ var wnCross=mt.linesCross(ln11x, ln11y, ln12x,ln12y, ln21x,ln21y, ln22x,ln22y) }
	wnCross=wnCross.x
	if((wnCross>ln11x and wnCross<ln12x) or (wnCross<ln11x and wnCross>ln12x)){
		if((wnCross>ln21x and wnCross<ln22x) or (wnCross<ln21x and wnCross>ln22x)){
		return true
	}}
	return false
}//<<
mt.linesCross=function(x11, y11, x12, y12, x21, y21, x22, y22) {
	var va0 = x12*y11-x11*y12
	var a1 = (y11-y12)/va0
	var b1 = (x12-x11)/va0
	var va = x22*y21-x21*y22
	var a2 = (y21-y22)/va
	var b2 = (x22-x21)/va
	return {x:(b1-b2)/(a2*b1-a1*b2), y:(a1-a2)/(b2*a1-b1*a2)}
}//<<
*/